package com.yangzc.lion.sys.organization.controller;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.lucene.queryparser.classic.ParseException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.yangzc.lion.common.controller.BaseController;
import com.yangzc.lion.common.dao.HQLInfo;
import com.yangzc.lion.common.service.IBaseService;
import com.yangzc.lion.common.tag.Page;
import com.yangzc.lion.search.model.SearchResultBean;
import com.yangzc.lion.search.util.IndexSearchUtil;
import com.yangzc.lion.sys.organization.interfaces.ISysOrgElement;
import com.yangzc.lion.sys.organization.interfaces.ISysOrgPerson;
import com.yangzc.lion.sys.organization.model.SysOrgElement;
import com.yangzc.lion.sys.organization.model.SysOrgPerson;
import com.yangzc.lion.sys.organization.service.ISysOrgPersonService;
import com.yangzc.lion.sys.util.StringUtil;

/**
 * 人员管理
 * 
 * @author yangzc
 * 
 */
@Controller
@RequestMapping("/sys/organization")
public class SysOrgPersonController extends BaseController<SysOrgPerson> {
	@Resource(name = "sysOrgPersonService")
	private ISysOrgPersonService sysOrgPersonService;

	@Override
	protected IBaseService<SysOrgPerson, String> getServiceImp() {
		return sysOrgPersonService;
	}
	
	/**
	 * 用户列表
	 * @param request
	 * @return
	 */
	@RequestMapping("/sysOrgPerson")
	public String sysOrgPerson(HttpServletRequest request) {
		Page<Map<String, Object>> page = super.findObjectPage(request);
		request.setAttribute("page", page);
		return "sys/organization/sysOrgPerson";
	}
	
	/**
	 * 跳转到新增用户
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/addSysOrgPerson")
	public String addSysOrgPerson(HttpServletRequest request,Model model) {
		String parent = request.getParameter("parent");
		if (!StringUtil.isNull(parent)) {
			ISysOrgElement parentModel = (ISysOrgElement) getServiceImp()
					.findByPrimaryKey(parent);
			model.addAttribute("fdParentId", parentModel.getFdId());
			model.addAttribute("fdParentName", parentModel.getFdName());
			model.addAttribute("method", "add");
		}
		return "sys/organization/editSysOrgPerson";
	}
	
	/**
	 * 编辑用户
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/editSysOrgPerson")
	public String editSysOrgPerson(HttpServletRequest request,Model model) {
		String fdId = request.getParameter("fdId");
		if (!StringUtil.isNull(fdId)) {
			SysOrgPerson person = (SysOrgPerson) getServiceImp()
					.findByPrimaryKey(fdId);
			model.addAttribute("method", "edit");
			model.addAttribute("person", person);
		}
		return "sys/organization/editSysOrgPerson";
	}
	
	/**
	 * 保存用户
	 * @param request
	 * @param modelObj 用户对象
	 * @return
	 */
	@RequestMapping("/saveSysOrgPerson")
	public String saveSysOrgPerson(HttpServletRequest request,SysOrgPerson modelObj) {
		if(StringUtil.isNotNull(modelObj.getFdId())){
			getServiceImp().update(modelObj);
		}
		else{
			getServiceImp().add(modelObj);
		}
		return "redirect:/sys/organization/sysOrgPerson.do";
	}
	
	
	/**
	 * 删除用户
	 * @param request
	 * @param modelObj 用户对象
	 * @return
	 */
	@RequestMapping("/delSysOrgPerson")
	public String delSysOrgPerson(HttpServletRequest request,HttpServletResponse response,String fdId) {
		if(StringUtil.isNotNull(fdId)){
			SysOrgPerson element = getServiceImp().findByPrimaryKey(fdId);
			try {
				getServiceImp().delete(element);
				response.setCharacterEncoding("utf-8");
				response.getWriter().write("true");
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	

	@RequestMapping("/getAllPerson.do")
	public String getPage(HttpServletRequest request) {
		Page<SysOrgPerson> page = this.findPage(request);
		return StringUtil.pageToString(page);
	}
	
	/**
	 * 搜索功能
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/search")
	public String sysOrgPerson(HttpServletRequest request,
			HttpServletResponse response) throws Exception, ParseException {
		String searchParam = request.getParameter("searchParam");
		if(StringUtil.isNotNull(searchParam)){
			String searchWhich = "fd_name";
			IndexSearchUtil isu = new IndexSearchUtil();
			List<SearchResultBean> results = null;
			results = isu.getSearchResult(searchWhich, searchParam);
			request.setAttribute("results", results);
		}
		return "search/searchList";
	}
	
	@Override
	protected void changeFindPageHQLInfo(HttpServletRequest request,
			HQLInfo hqlInfo) {
		String fdName = request.getParameter("fdName");
		if(StringUtil.isNotNull(fdName)){
			hqlInfo.setWhereBlock("sysOrgPerson.fdName like :fdName");
			hqlInfo.setParameter("fdName", "%"+fdName+"%");
			request.setAttribute("fdName", fdName);
		}
		hqlInfo.setSelectBlock("sysOrgPerson.fdId as fdId,sysOrgPerson.fdName as fdName,sysOrgPerson.fdLoginName as fdLoginName,sysOrgPerson.fdCreateTime as fdCreateTime,sysOrgPerson.fdAlterTime as fdAlterTime");
		super.changeFindPageHQLInfo(request, hqlInfo);
	}

}